package com.suptrack.web.servlet;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.suptrack.entity.User;
import com.suptrack.entity.Zone;
import com.suptrack.service.CarService;
import com.suptrack.service.UserService;
import com.suptrack.service.ZoneService;

/**
 * Servlet implementation class addZoneServlet
 */
@WebServlet("/RegisteredContent/addZone")
public class addZoneServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	@EJB
	ZoneService zoneservice;
	@EJB
	UserService userservice;
	@EJB
	CarService carservice;
 
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String _name = request.getParameter("Z_name");
		String _radius = request.getParameter("Z_radius");
		String _position = request.getParameter("position");
		String idCar = request.getParameter("Zcar");
		System.out.println(_position);
		Zone zonetemp = new Zone();
		zonetemp.setName(_name);
		zonetemp.setRadius(Double.parseDouble(_radius));
	
		String _positionx = _position.substring(0, _position.indexOf(","));
		String _positiony = _position.substring(_position.indexOf(", ")+2,_position.length());
		zonetemp.setPositionx(Double.parseDouble(_positionx));
		zonetemp.setPositiony(Double.parseDouble(_positiony));
		zonetemp.setCar(carservice.getCarInfoService(Long.parseLong(idCar)));
		
		System.out.println("zone name : "+_name);
		System.out.println("radius in double : "+Double.parseDouble(_radius));
		System.out.println("x:"+Double.parseDouble(_positionx));
		System.out.println("y:"+Double.parseDouble(_positiony));
		zoneservice.addZoneService(zonetemp);
		
		response.sendRedirect(request.getServletContext().getContextPath()+"/RegisteredContent/ManageZone");
		
	}

}
